package com.itheima.sh.a_axios_json_01;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/json2Servlet")
public class Json2Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //需求2，请求是普通字符串，响应数据改成json字符串
        //1。获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //2.输出
        System.out.println(username+"----"+password);
        //3。创建User对象
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        //4.使用fastjson将user对象转化为json字符串
        String s = JSON.toJSONString(user);
        //5.响应给浏览器
        response.getWriter().print(s);
    }
}